#include "musicslider.h"
#include "ui_musicslider.h"
#include<QMouseEvent>

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
    maxWidth=this->width();
    currPos=0;
    moveSlider();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::setStep(float ratio)
{
    currPos=maxWidth*ratio;
    moveSlider();

}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    const QRect musicSliderRect=QRect(0,0,geometry().width(),geometry().height());
    if(!musicSliderRect.contains(event->pos())){
        return;
    }

    if(event->buttons()==Qt::LeftButton){
        currPos=event->pos().x();
//        if(currPos<0)currPos=0;
        if(currPos>maxWidth)currPos=maxWidth;
        moveSlider();
    }


}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    currPos=event->pos().x();
    moveSlider();
}

void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{

    currPos=event->pos().x();
    moveSlider();
    setMusicSliderPosition(ui->outLine->width()/(float)maxWidth);
}

void MusicSlider::moveSlider()
{

    ui->outLine->setGeometry(ui->outLine->x(), ui->outLine->y(), currPos, ui->outLine->height());
}
